<?php
/*
 * @Author: 哲学家乔布斯 <wuyangii@qq.com>
 * @Date: 2022-10-20 21:41:37
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-11-07 22:37:15
 * @Descripttion: 系统配置
 */

namespace app\api\controller\user\v1_0\bus;

use think\Db;

class Withdrawal extends \app\api\controller\user\v1_0\Common
{
    protected $noNeedRight = ['test'];
    /**
     * @Author: 哲学家乔布斯 <wuyangii@qq.com>
     * @brief: 
     * @return 
     * @Descripttion: 提现接口
     */    
    public function index(){
        $user = (new \app\common\model\User())->where(['id' => $this->UserInfo['id']])->find();
        $bus = (new \app\common\model\Bus())->where(['id' => $user['bus_id']])->find();
        $page = empty(request()->param('page')) ? 1 : request()->param('page');
        $limit = empty(request()->param('limit')) ? 10 : request()->param('limit');
        if(empty($bus)){
            return self::errorResult("当前用户暂未开启该功能");
        }
        $list = (new \app\common\model\BusWithdrawal())
                ->where(['bus_id' => $bus['id']])
                ->append(['addtime_text','state_text'])
                ->page($page)
                ->limit($limit)
                ->select();
        return self::successResult("成功",[
            'money' => [
                'bus_smoney' => $bus['money'],
            ],
            'list' => $list
        ]);
    }
    /**
     * @Author: 哲学家乔布斯 <wuyangii@qq.com>
     * @brief: 
     * @return 
     * @Descripttion: 提现申请提现
     */ 
    public function add(){
        $param = request()->param();
        $user = (new \app\common\model\User())->where(['id' => $this->UserInfo['id']])->find();
        $bus = (new \app\common\model\Bus())->where(['id' => $user['bus_id']])->find();
        $alipay = (new \app\common\model\BusAlipay())->where(['bus_id' => $user['bus_id']])->find();
        $withdrawal_sun = aconfig('withdrawal_sun',$this->agent_id);
        $withdrawal_low = aconfig('withdrawal_low',$this->agent_id);
        $date_start = strtotime(date("Ymd")."00:00:00");
        $date_end = strtotime(date("Ymd")."23:59:59");
        $withdrawal_count = (new \app\common\model\BusWithdrawal())
                    ->where('addtime',['>',$date_start],['<',$date_end],'and')
                    ->count();

        if(empty($alipay)){
            return self::errorResult("请绑定支付宝信息",[],1001);
        }
        if($param['money'] > $bus['money']){
            return self::errorResult("余额不足");
        }
        if($withdrawal_sun != 0 && $withdrawal_count < $withdrawal_sun){
            return self::errorResult("今日提现已达到上限！！！");
        }
        if($withdrawal_low != 0 && $param['money'] > $withdrawal_low){
            return self::errorResult("不能低于{$withdrawal_low}");
        }
        $order_on = 'T' . date('YmdHis') . rand(1000, 9999);
        Db::startTrans();
        try{
            $message = "提现失败,请重试";
            $process = true;
            
            $row = (new \app\common\model\BusWithdrawal())->insert([
                'agent_id' => $this->agent_id,
                'bus_id' => $bus['id'],
                'user_id' => $user['id'],
                'money' => $param['money'],
                'alipay_id' => $alipay['id'],
                'alipay_name' => $alipay['name'],
                'alipay_phone' => $alipay['phone'],
                'notes' => $alipay['notes'],
                'order_on' => $order_on,
                'addtime' => time(),
            ]);
            if(!$row){
                $message = "创建提现记录申请,请重试";
                $process = false;
            }
            #-------------------------------修改提货点资金日志-----------------------
            $row = (new \app\common\model\Bus())->where([
                'id' => $bus['id'],
                'money' => $bus['money']
            ])->update([
                'money' => $bus['money']-$param['money']
            ]);
            if(!$row){
                $message = "修改资金失败,请重试";
                $process = false;
            }
            $row = (new \app\common\model\BusMoneyLog())->insert([
                'title' => "提现操作",
                'bus_id' => $bus['id'],
                'type' => 1,
                'money' => $param['money'],
                'addtime' => time(),
                'order_id' => 0,
                ]); 
            if(!$row){
                $message = "修改资金日志失败,请重试";
                $process = false;
            }
          
            if ($process !== false) {
                Db::commit(); // 提交事务
            } else {
                Db::rollback(); // 回滚事务
            }
        } catch (\Exception $e) {
            $process = false;
            $message = $e->getMessage();
            Db::rollback(); // 回滚事务
        }
        if ($process !== false) {
            return self::successResult("提交成功");
        } else {
            return self::errorResult($message);
        }
        
        
      

    }
 
    
    

}
